using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace NegBinary
{
    public partial class FMain : Form
    {
        public FMain()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MultiplyTest();
            return;
            for (int iIdx = -64; iIdx < 128; iIdx++)
            {
                string sHexNega = RapidHDL.Conversion.StringBinaryToHex(Convert.LongToNegBinary(iIdx, 10));
                string sHexBinary = RapidHDL.Conversion.StringBinaryToHex(RapidHDL.Conversion.SignedLongToBinaryString(iIdx, 10));
                
                string sForward = "test_converter(0x" + sHexNega + ",0,0x" + sHexBinary + ");";
                string sBackward = "test_converter(0x" + sHexBinary + ",1,0x" + sHexNega + ");";

                if (sHexNega[0] == '0')
                    Console.WriteLine(sForward);

                if (sHexBinary[0] == '0')
                    Console.WriteLine(sBackward);

                //Console.WriteLine(iIdx.ToString() + " -- " + Convert.LongToNegBinary(iIdx, 10) + " -- " + RapidHDL.Conversion.SignedLongToBinaryString(iIdx, 10) + "  {" + sHex + " = " + sHex2 + "}");
            }

            Console.WriteLine(Convert.LongToNegBinary(-42*85,16));
            Console.WriteLine(Convert.LongToNegBinary(85*85,16));
            Console.WriteLine(Convert.LongToNegBinary(-42 * -42, 16));
        }

        private void MultiplyTest()
        {
            long lTest = 0;
            long lHighRange;
            bool bOverflow = false;

            while (!bOverflow)
            {
                try
                {
                    string sTest = Convert.LongToNegBinary(lTest + 1, 4);
                    lTest = lTest + 1;
                }
                catch
                {
                    bOverflow = true;
                }
            }
            lHighRange = lTest;

            bOverflow = false;
            while (!bOverflow)
            {
                try
                {
                    string sTest = Convert.LongToNegBinary(lTest - 1, 4);
                    lTest = lTest - 1;
                }
                catch
                {
                    bOverflow = true;
                }
            }
            long lLowRange = lTest;

            for (long lA = lLowRange; lA <= lHighRange; lA++)
            {
                for (long lB = lLowRange; lB <= lHighRange; lB++)
                {
                    long lAnswer = lA * lB;
                    string sA = RapidHDL.Conversion.StringBinaryToHex(Convert.LongToNegBinary(lA, 4));
                    string sB = RapidHDL.Conversion.StringBinaryToHex(Convert.LongToNegBinary(lB, 4));
                    string sResult = Convert.LongToNegBinary(lAnswer, 4 * 2 + 1);
                    string sTestResult = sResult[0].ToString() + sResult[3].ToString() + sResult[8].ToString();
                    string sTest = RapidHDL.Conversion.StringBinaryToHex(sTestResult);
                    string sCommand = "test_multiplier(0x" + sA.ToString() + ",0x" + sB.ToString() + ",0x" + sTest.ToString() + ");";
                    System.Console.WriteLine(sCommand);
                }
            }


        }

        NegBinaryPadFrame oHardware;

        private void button2_Click(object sender, EventArgs e)
        {
            oHardware = new NegBinaryPadFrame();
            oHardware.StartRapidHardware();
            oHardware.LaunchGUI(false);
        }

        NegBinary2CompPadFrame oNeg2CompHardware;
        private void button3_Click(object sender, EventArgs e)
        {
            oNeg2CompHardware = new NegBinary2CompPadFrame();
            oNeg2CompHardware.StartRapidHardware();
            oNeg2CompHardware.LaunchGUI(false);
        }
    }
}